package com.goldcn.oss.controller;

import com.goldcn.oss.service.OssService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2016/8/12.
 */
@RestController
public class OSSController {

    @Autowired
    OssService ossService;

    /**
     * oss 文件上传
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/oss/file")
    public Object uploadfile(HttpServletResponse response, HttpServletRequest request,
                             @RequestParam(value = "file", required = false) MultipartFile file,
                             @RequestParam(value = "type", required = false) String type){
        return JSONObject.fromObject(ossService.uploadFiles(file));
    }

    /**
     * oss 文件下载
     * @param fileName
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET,value = "/oss/file/{fileName:.+}")
    public Object downloadFile(@PathVariable String fileName, HttpServletResponse response){
        try {
            response.setContentType("text/html; charset=utf-8");
            ossService.downloadFile(response,fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "{code:'success'}";
    }

    /**
     * oss 文件删除
     * @param fileName
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE,value = "/oss/file/{fileName:.+}")
    public Object deleteFile(@PathVariable String fileName){
        return JSONObject.fromObject(ossService.deleteFile(fileName)).toString();
    }

    /**
     * 图片base64 （h5）
     *
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/oss/base64", method = RequestMethod.POST)
    public Object base64Img(HttpServletRequest request,
                            @RequestParam(required = false,value = "Orientation")  String Orientation) {
        String base64Code = request.getParameter("base64");
        return ossService.uploadFiles(base64Code,Orientation);
    }


    /**
     * 图片byte
     *
     * @param
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/oss/byte", method = RequestMethod.POST)
    public Object byteImg(@RequestParam(required = false,value = "Orientation")  byte[] buff) {

        return ossService.uploadFilesByte(buff);
    }

    /**
     * oss 文件上传
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/oss/video")
    public Object videoUploadfile(HttpServletResponse response, HttpServletRequest request,
                             @RequestParam(value = "file", required = false) MultipartFile file){
        return JSONObject.fromObject(ossService.videoUploadFiles(file));
    }

}
